package com.ruoyi.qualityControl.controller;

import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.domain.vo.InitTableVo;
import com.ruoyi.common.service.TableService;
import com.ruoyi.logistic.domain.PageInfo;
import com.ruoyi.qualityControl.domain.*;
import com.ruoyi.qualityControl.domain.vo.QcTimeExamineCollectVo;
import com.ruoyi.qualityControl.service.QcTimeExamineService;
import com.ruoyi.qualityControl.utils.GetDate;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@RequestMapping("/qc/timeExamine")
@RestController
@Api(tags = "质控中心_时效考核")
public class QcTimeExamineController {
    @Autowired
    private QcTimeExamineService qcTimeExamineService;
    @Autowired
    private TableService tableService;

    @ApiOperation("汇总")
    @PostMapping("/collect")
    public R collect(@RequestBody QcTimeExamineCollectVo vo) {
        if (vo.getStartTime() == null || vo.getEndTime() == null || vo.getNetworkNameId() == null) {
            return R.fail("请输入查询所需的时间参数和网点信息!");
        }
        QcTimeExamineAll allList = new QcTimeExamineAll();
        List<QcTimeExamineCollect> list = new ArrayList<>();
        if (vo.getType().equals("receive")) {
            list.add(qcTimeExamineService.collectReceive(vo));
            list.add(qcTimeExamineService.collectStranded(vo));
            list.add(qcTimeExamineService.collectReturnGood(vo));
            list.add(qcTimeExamineService.collectSign(vo));
            allList.setObjects(list);
            allList.setSheets(qcTimeExamineService.receiveSheet(vo));
            String[] fields = {"collectDate", "networkName","contractAreaName"
                    , "orderNum", "pickupTimelyNum", "pickupTimelyRate", "pickupNotTimelyNum", "pickupNotTimelyRate", "pickupCostAvg"
                    , "pickupBeforePlaceOrderNum"};
            InitTableVo initTableVo = new InitTableVo();
            initTableVo.setFields(fields);
            initTableVo.setCode("qcTimeExamineReceive");
            allList.setFields(tableService.initTable(initTableVo));
            return R.ok(allList);
        }
        if (vo.getType().equals("stranded")) {
            list.add(qcTimeExamineService.collectReceive(vo));
            list.add(qcTimeExamineService.collectStranded(vo));
            list.add(qcTimeExamineService.collectReturnGood(vo));
            list.add(qcTimeExamineService.collectSign(vo));
            allList.setObjects(list);
            allList.setSheets(qcTimeExamineService.stranded(vo));
            String[] fields = {"collectDate", "networkName","contractAreaName","startPickupNum","totalPickupNum","totalPickupNum"
                    , "totalStrandedRate", "examinePickupNum", "examineStrandedNum", "examineStrandedRate", "examineAimValue", "examinePreStranded"
                    , "examineOverFortyEightNum", "receiveOverFortyEightNum", "receiveOverFortyEightDeliveredNum", "receiveOverFortyEightUnDeliveredNum"
                    , "frontDeliveryEvaluatePassNum", "frontDeliveryEvaluateNotPassNum", "frontNotDeliveryNum", "stagnantTwentyFourNum",
                    "stagnantTwentyFourRate", "detainedOrStagnantTwentyFourNum", "detainedOrUnStagnantTwentyFour", "avgFrontDeliveryDeliveryTimeUsed"};
            InitTableVo initTableVo = new InitTableVo();
            initTableVo.setFields(fields);
            initTableVo.setCode("qcTimeExamineStranded");
            allList.setFields(tableService.initTable(initTableVo));
            return R.ok(allList);
        }
        if (vo.getType().equals("returnGood")) {
            list.add(qcTimeExamineService.collectReceive(vo));
            list.add(qcTimeExamineService.collectStranded(vo));
            list.add(qcTimeExamineService.collectReturnGood(vo));
            list.add(qcTimeExamineService.collectSign(vo));
            allList.setObjects(list);
            allList.setSheets(qcTimeExamineService.returnGoodSheet(vo));
            String[] fields = {"collectDate", "networkName","contractAreaName"
                    , "frequency", "centreSentNum", "examineNum", "returnGoodTimelyNum", "returnGoodTimelyRate", "returnGoodHoldupNum"
                    , "returnGoodHoldupRate", "wrongSentNextNetworkNum"};
            InitTableVo initTableVo = new InitTableVo();
            initTableVo.setFields(fields);
            initTableVo.setCode("qcTimeExamineReturnGood");
            allList.setFields(tableService.initTable(initTableVo));
            return R.ok(allList);

        }
        if (vo.getType().equals("sign")) {
            list.add(qcTimeExamineService.collectReceive(vo));
            list.add(qcTimeExamineService.collectStranded(vo));
            list.add(qcTimeExamineService.collectReturnGood(vo));
            list.add(qcTimeExamineService.collectSign(vo));
            allList.setObjects(list);
            allList.setSheets(qcTimeExamineService.signSheet(vo));
            String[] fields = {"collectDate", "networkName","contractAreaName","hqSolicitor","hqTransferCentre"
                    , "paiTimes", "preExamineAmount", "p1ExamineNum", "p1SignNum", "p1SignRate", "p1AimValues"
                    , "p1PreExamineAmount", "p1UnTimelySignNum", "p2ExamineNum", "p2SignNum", "p2SignRate"
                    , "p2AimValues", "p2PreExamineAmount", "p2UnTimelySignNum", "p2T0DelaySignNum", "p2T1SignNum", "p2T2SignNum", "p2SignRate"
                    , "p2T3SignNum", "toExamineNum", "toTimelySignNum", "toTimelySignRate", "toAimValues", "toPreExamineAmount", "toUnTimelySignNum"
                    , "toSignDelay0To24Num", "toSignDelay0To24Rate", "toSignDelay24To48Num", "toSignDelay24To48Rate", "toSignDelayOver48Num", "toSignDelayOver48Rate", "sendSignTime"
                    , "networkNeedTime", "miSignNum", "miUnSignNum", "miEndPaiNum", "miSignSchedule", "miUnSignProblemNum"};
            InitTableVo initTableVo = new InitTableVo();
            initTableVo.setFields(fields);
            initTableVo.setCode("qcTimeExamineSign");
            allList.setFields(tableService.initTable(initTableVo));
            return R.ok(allList);

        }
        if (vo.getType().equals("")) {
            list.add(qcTimeExamineService.collectReceive(vo));
            list.add(qcTimeExamineService.collectStranded(vo));
            list.add(qcTimeExamineService.collectReturnGood(vo));
            list.add(qcTimeExamineService.collectSign(vo));
            allList.setObjects(list);
            allList.setSheets(qcTimeExamineService.receiveSheet(vo));
            String[] fields = {"collectDate", "networkName","contractAreaName"
                    , "orderNum", "pickupTimelyNum", "pickupTimelyRate", "pickupNotTimelyNum", "pickupNotTimelyRate", "pickupCostAvg"
                    , "pickupBeforePlaceOrderNum"};
//            , "deliveryLateSign", "noAccuracyRate", "arrAfterSignCount", "arrIssuePerStr"
            InitTableVo initTableVo = new InitTableVo();
            initTableVo.setFields(fields);
            initTableVo.setCode("qcTimeExamineReceive");
            allList.setFields(tableService.initTable(initTableVo));
            return R.ok(allList);
        }
        return R.fail("暂无该参数信息");
    }
}

